// pages/register-Page/register-Page.js
Page({
    data: {
        showLoadingState:false,
        //图标和文字 
        p1:"https://pic1.imgdb.cn/item/633c1f0716f2c2beb11fdf85.png",
        p2:"https://pic1.imgdb.cn/item/633a49e916f2c2beb14946cc.png",   
        //身份
        p3:"https://pic1.imgdb.cn/item/633a49e916f2c2beb14946d5.png",
        loginModeArray:["请选择身份","用户","管理员"],
        loginModeNum:0,
        //姓名 学工号 密码 密码2
        p4:"https://pic1.imgdb.cn/item/633a49e916f2c2beb14946db.png",
        p5:"https://pic1.imgdb.cn/item/633a49e916f2c2beb14946c0.png",
        p6:"https://pic1.imgdb.cn/item/633a4a3b16f2c2beb149c4ba.png",
        name:"",
        studentId:123,
        password:"",
        password2:"",
        //注册
        p8:"https://pic1.imgdb.cn/item/633a4a3b16f2c2beb149c4bd.png",
    },
    onLoad(options) {

    },
    onChangeLoginMode:function(e){
        this.setData({
            'loginModeNum':e.detail.value,
        });
    },
    onChangeName:function(e){
        this.setData({
            "name":e.detail.value,
        })
    },
    onChangeId:function(e){
        this.setData({
            "studentId":e.detail.value,
        });
    },
    onChangePassword:function(e){
        this.setData({
            "password":e.detail.value,
        });
    },
    onChangePassword2:function(e){
        this.setData({
            "password2":e.detail.value,
        })
    },
    onRegister:function(e){
        var that = this;
        if(that.data.password != that.data.password2){
            wx.showModal({
              showCancel:false,
              title:"您的两次密码不一致，请重新修改！",
            });
        }
        else if(that.data.loginModeNum == 0){
            wx.showModal({
                showCancel:false,
                title:"请选择身份",
            })
        }
        else if(that.data.loginModeNum == 1){
            that.onShowLoading()
            wx.request({
                url: getApp().globalData.serverUrl,
                method:"GET",
                timeout:10000,
                data:{
                    operation:"REGISTERUSER",
                    userName:that.data.name,
                    password:that.data.password,
                    studentId:that.data.studentId,
                },
                success(res){

                  if(res.data == true){
                      that.onHideLoading()
                      console.log(res);
                      wx.showModal({
                        showCancel:false,
                        title:"注册成功",
                        success(res){
                            if(res.confirm){
                                wx.navigateBack();
                            }
                        }
                      })
                  }
                  else {
                    that.onHideLoading()
                    wx.showModal({
                        showCancel:false,
                        title:"请求失败,请重试",
                    });
                }
                },
                fail(res){
                    that.onHideLoading()
                  wx.showModal({
                      showCancel:false,
                      title:"请求超时,请重试",
                  });
                },
              });
        }
        else if(that.data.loginModeNum == 2){
            that.onShowLoading()
            wx.request({
                url: getApp().globalData.serverUrl,
                method:"GET",
                timeout:10000,
                data:{
                    operation:"REGISTERMANAGER",
                    userName:that.data.name,
                    password:that.data.password,
                    studentId:that.data.studentId,
                },
                success(res){
                  if(res.data == false){
                      that.onHideLoading()
                      wx.showModal({
                          showCancel:false,
                          title:"请求失败,请重试",
                      });
                  }
                  else{
                      that.onHideLoading()
                    console.log(res);
                    wx.showModal({
                      showCancel:false,
                      title:"注册成功",
                      success(res){
                          if(res.confirm){
                              wx.navigateBack();
                          }
                      }
                    })
                  }
                },
                fail(res){
                    that.onHideLoading();
                  wx.showModal({
                      showCancel:false,
                      title:"请求超时,请重试",
                  });
                },
              });
        };
    },
    onShowLoading:function(e){
        var that = this;
        wx.showLoading({
          title: '加载中',
          mask:true,
          success(res){
              that.setData({
                  "showLoadingState":true,
              })
          }
        })
        setTimeout(that.onHideLoading,10200)
    },
    onHideLoading:function(e){
        var that = this;
        if(that.data.showLoadingState == true){
            wx.hideLoading();
            that.setData({ 
                "showLoadingState":false,
            })
        }
    },
})